﻿var fm = Ext.form;
var Ed = Ext.grid.GridEditor;

Ext.data.Store.prototype.currentGroup ="" ;
Ext.data.Store.prototype.currentFormat = 'scheda-uno' ;
Ext.BLANK_IMAGE_URL = "images/s.gif";
Ext.GRID_PAGE_SIZE = 17 ;
 
/*****************************************************************
						RECORDS
*****************************************************************/
 

var opere = function() {  
	var tmpRecord = Ext.data.Record.create([ 
			{name: 'Id', mapping: 'Id'},
			{name: 'Dtrealizzazione', mapping: 'Dtrealizzazione'} ,
			{name: 'Dtrilievo', mapping: 'Dtrilievo'} , 
			{name: 'Idstrada', mapping: 'Idstrada'} ,
			{name: 'Tipo', mapping: 'Tipo'} ,
			{name: 'Numero', mapping: 'Numero'} ,
			{name: 'Estensione', mapping: 'Estensione'} ,
			{name: 'DescStrada', mapping: 'DescStrada'} ,
			{name: 'DesEstesaStrada', mapping: 'DesEstesaStrada'} ,
			{name: 'Idcomune', mapping: 'Idcomune'} ,
			{name: 'DescComune', mapping: 'DescComune'} ,
			{name: 'Siglaprovincia', mapping: 'Siglaprovincia'} ,
			{name: 'DesEstesaComune', mapping: 'DesEstesaComune'} ,
			{name: 'Localita', mapping: 'Localita'} ,
			{name: 'Km', mapping: 'Km'} ,
			{name: 'Metri', mapping: 'Metri'} ,
			{name: 'Longitudine', mapping: 'Longitudine'  , type: 'string'} ,
			{name: 'Latitudine', mapping: 'Latitudine'  , type: 'string'} ,
			{name: 'Distanzasedestradale', mapping: 'Distanzasedestradale'} ,
			{name: 'Quotaslm', mapping: 'Quotaslm'} ,
			{name: 'Note', mapping: 'Note'} 
		]);
	
	
	var tmpDs = new Ext.data.Store({
			id :'ds-opere-grid',
			remoteSort:true, 
			baseParams:{action:'list'},
			record: tmpRecord   , 
			url: "opere/data.php" ,  
			reader : new Ext.data.JsonReader(
						{ root: 'rows',  totalProperty: 'results',  id: 'rowId' }, 
						tmpRecord 
			)
			
		}) ;
	
	
	var tmpNewRecord = function(data){
		data = {}||data;
		var emptyRecord = {} ;	  
		for(fld in tmpDs.fields.map ) emptyRecord[fld]='';
		for(fld in data)	 emptyRecord[fld]=data[fld];
		return new tmpDs.recordType(emptyRecord) ;
	}
	
	return { 
		newRecord:tmpNewRecord,
		record : tmpRecord , 
		ds : tmpDs
	}; // return	
}();
 

var  strade= function(){
	var tmpRecord = Ext.data.Record.create([ 
		{name: 'Id', mapping: 'Id'}, 
		{name: 'Tipo', mapping: 'Tipo'},
		{name: 'Numero', mapping: 'Numero'},
		{name: 'Estensione', mapping: 'Estensione'},
		{name: 'Nome', mapping: 'Nome'},
		{name: 'Descrizione', mapping: 'Descrizione'},
		{name: 'Note', mapping: 'Note'} 
	])
	
	return {
		record : tmpRecord  
	}
}();
	
var comuni = function(){
	var tmpRecord = Ext.data.Record.create([ 
		{name: 'Id', mapping: 'Id'},
		{name: 'Nome', mapping: 'Nome'},
		{name: 'Siglaprovincia', mapping: 'Siglaprovincia'},
		{name: 'Descrizione', mapping: 'Descrizione'},
		{name: 'Note', mapping: 'Note'} 
	]);
	
	return {
		record:tmpRecord
	}
}();	
	
var tipi = function(){
	
	var tmpColumnModel = function(){
		return  new Ext.grid.ColumnModel( [ 
			new Ext.grid.RowNumberer(),
			{  sortable:true , header: "Tipo", dataIndex: 'Tipo', width: 90, editor: new Ed(new fm.TextField({ allowBlank: false })) },
			{  sortable:true , header: "Descrizione", dataIndex: 'Descrizione', width: 140, editor: new Ed(new fm.TextField({ allowBlank: false })) },
			{  header: "Immagine", dataIndex: 'Immagine', width: 90, editor: new Ed(new fm.TextField({ allowBlank: true })) }
			//,{ header: "Note", dataIndex: 'Note', width: 180, editor: new Ed(new fm.TextField({ allowBlank: true })) }
		]);
	};	

	var tmpRecord = function(){
		return Ext.data.Record.create([ 
			{name: 'Id', mapping: 'Id'},
			{name: 'Tipo', mapping: 'Tipo',type: 'string'},
			{name: 'Descrizione', mapping: 'Descrizione', type: 'string'},
			{name: 'Note', mapping: 'Note', type: 'string'},
			{name: 'Immagine', mapping: 'Immagine', type: 'string'}
		]) ;
	} 	
	
	var tmpDs = function(config){
		
		var locTmpDs = new Ext.data.Store({
			id :"ds-grid-" + config.id,
			remoteSort:false, 
			baseParams:{action:'list',table:config.id},
			record: tmpRecord()   , 
			proxy : new Ext.data.HttpProxy({ url:config.url} ),  
			reader : new Ext.data.JsonReader(
						{ root: 'rows',  totalProperty: 'results',  id: 'rowId' }, 
						tmpRecord()
			)
			
		}) ;
		 
		return locTmpDs;
	};	
		
		
	var tmpWindow =  function(config){		
		var url = 'tipi/data.php' ;
		
		var tmpGrid={
			id:config.id,
			postUrl:url,
			clicksToEdit:0 ,	
			border:false,
			xtype:'editorextgrid',
			frame:true, 
			ds: tmpDs({id:config.id,url:url}),
			cm: tmpColumnModel(),
			viewConfig: { forceFit:true }
		};
	
		return  { 
			iconCls:'icon-list',
			id:'window-'+config.id,
			width:600,
			height:340,
			title:config.title, 
			closeAction:'hide',
			layout: 'fit',
			items : [tmpGrid]
		};
		 
	}
	
	return { window : tmpWindow };
}();
	

var talimentazioni = { window:tipi.window({id:'Tipialimentazione' , title:'Tipi Alimentazione'}) };
var tancoraggi = { window:tipi.window({id:'Tipiancoraggio' , title:'Tipi Ancoraggio'}) };
var tchiodi = { window:tipi.window({id:'Tipichiodo' , title:'Tipi Chiodi'}) };
var tconsolidamento = { window:tipi.window({id:'Tipiconsolidamento' , title:'Tipi Consolidamento'}) };
var tdissipatore = { window:tipi.window({id:'Tipidissipatore' , title:'Tipi Dissipatore'}) };
var telemento = { window:tipi.window({id:'Tipielemento' , title:'Tipi Elemento'}) };
var tfondazione = { window:tipi.window({id:'Tipifondazione' , title:'Tipi Fondazione'}) };
var tgradone = { window:tipi.window({id:'Tipigradone' , title:'Tipi Gradone'}) }; 
var tprofilo = { window:tipi.window({id:'Tipiprofilo' , title:'Tipi Profilo'}) };
var trete = { window:tipi.window({id:'Tipirete' , title:'Tipi Rete'}) };
var ttrasmissione = { window:tipi.window({id:'Tipitrasmissione' , title:'Tipi Trasmissione'}) };
var tvallo = { window:tipi.window({id:'Tipivallo' , title:'Tipi Vallo'}) };
 

Ext.ux.resources =  {	
	confirm :"Confirm",
	status :"Status"
};

var app = function(){ 
	return {
		pagedGridLoadParameters : {  
			'params' : {start:0, limit: Ext.GRID_PAGE_SIZE}  ,   
			callback:function(rec,options,success){ } 
		} , 
		showGridTipiWindow : function(parameters){
			var curw = Ext.getCmp(parameters.id) ;
			if(curw==null) { 
				curw = new Ext.Window(parameters); 
			}
			curw.show();
		},
		showOpereWindow : function(record){
			Ext.get(document.body).mask("Caricamento dati Opera in corso...");
				var windSchede  = Ext.getCmp("window-schede");
				var frm = Ext.getCmp("schede-panel");
				frm.loadRecord(   record  );
				 
				windSchede.show();
				 
			Ext.get(document.body).unmask();	
		}
	}	
}();

 

Ext.onReady(function(){
	Ext.get(document.body).mask("Caricamento in corso...");
	Ext.QuickTips.init();
		 
	
	var title = "Gestione Opere" ;
	document.title = title ;  
	opere.ds.load(  app.pagedGridLoadParameters  ); 
	    
	  
	Ext.Ajax.request({url : 'opere/window.def' , 
		extraParams: {time:new Date},
		method:'POST',
		success: function ( resp, opt ) {
			  
			eval(  "new Ext.Window (" + resp.responseText  + ").render(document.body)"  ) ;  
		}, 
		failure: function ( resp, opt ) { alert("Errore : opere/window.def  " ) } 
	}); 
	
	var viewport = new Ext.Viewport({ 
		layout:'border',  
		items: [{
				xtype:'panel',
				id:'application-left-panel',
				region:'west',
				margins:'0 0 0 0',
				split:true,
				collapsible: true,
				collapseMode: 'mini',
				width:250,
				minWidth: 250,
				maxWidth: 250,
				border: true,
				baseCls:'x-plain', 				
				items : [{ 
						frame:true,
						title:'Veneto Strade - Gestione opere'
					},
					{ //application-left-panel
						xtype:'tabpanel',
						border:false,
						activeTab: 0,
						bodyStyle:'padding:2px;margin:0', 
						items :[{
							title:'Opere' ,
							frame:true,
							autoHeight:true,
							tbar:  [{
										text:' Aggiungi Opera',
										iconCls: 'add' ,
										handler:function(){
											var trecord = opere.newRecord();
											app.showOpereWindow(trecord);
										}
									}],
									
							items:[{
								xtype:'form',
								iconCls:'icon-search',	
								title: 'Ricerca Opere',
								collapsible:true,
								id:'schede-search-panel',
								formId:'schede-search-form',
								labelWidth : 65,
								defaultType: 'textfield',
								frame:true,
								
								items : [
									{ fieldLabel: 'N° Strada', name: 'in__Numero' , width:80},
									{ fieldLabel: 'Strada', name: 'vc__strade/Nome' , width:133},
				                    { fieldLabel: 'Comune', name: 'vc__comuni/Nome' , width:133},
				                    { fieldLabel: 'Località', name: 'vc__Localita' , width:133} 
									
								],
								
								buttons: [{ 	
										text: 'Nuova Ricerca', handler : function(){
											var thisForm = Ext.getCmp("schede-search-panel").form ; 
											thisForm.reset();
											  
										}
									},
									{ 	id:'button-schede-search-trova',
										text: 'Trova' , handler : function(){
											var gridPanel = Ext.getCmp("schede-grid") ;
											var thisStore =  gridPanel.store ; 
											var thisForm = Ext.getCmp("schede-search-panel").form ; 
											var values = thisForm.getValues(false) ;
											 
											for(val in values) thisStore.baseParams[val] = values[val];
											thisStore.baseParams["action"] ='list' ; 
											
											thisStore.load(   app.pagedGridLoadParameters   );
											 
										} 
									}]//buttons
									
								
							}]
						}, 
						{
							title:'Tabelle tipi',
							frame:true,
							border:false,
							items :[{ //****   treepanel
									xtype:'treepanel',
									id:'im-tree', 
									loader: new Ext.tree.TreeLoader(),
									rootVisible:false,
									lines:false,
									autoScroll:true,
									border:false,	
									frame:true,
									root: new Ext.tree.AsyncTreeNode({
										text:'Online',
										
										//*************************    children
										children:[{   	
												text:'Tabelle', expanded:true,
												children:[	 
														   { text:'Tipi alimentazione' , leaf:true , iconCls:'icon-confg' , href:"javascript:app.showGridTipiWindow(talimentazioni.window)"},
														   { text:'Tipi ancoraggio in fune' , leaf:true , iconCls:'icon-ancoraggio',href:"javascript:app.showGridTipiWindow(tancoraggi.window)"},
														   { text:'Tipi chiodo' , leaf:true , iconCls:'icon-chiodi',href:"javascript:app.showGridTipiWindow(tchiodi.window)"},
														   { text:'Tipi consolidamento' , leaf:true , iconCls:'icon-consolidamento',href:"javascript:app.showGridTipiWindow(tconsolidamento.window)"},
														   { text:'Tipi dissipatore' , leaf:true , iconCls:'icon-dissipatore',href:"javascript:app.showGridTipiWindow(tdissipatore.window)"},
														   { text:'Tipi elemento' , leaf:true , iconCls:'icon-elemento',href:"javascript:app.showGridTipiWindow(telemento.window)"},
														   { text:'Tipi fondazione' , leaf:true , iconCls:'icon-fondazione',href:"javascript:app.showGridTipiWindow(tfondazione.window)"},
														   { text:'Tipi gradone' , leaf:true , iconCls:'icon-gradone',href:"javascript:app.showGridTipiWindow(tgradone.window)"},
														   { text:'Tipi opere' , leaf:true , iconCls:'icon-chiave'},
														   { text:'Tipi profilo' , leaf:true , iconCls:'icon-profillo',href:"javascript:app.showGridTipiWindow(tprofilo.window)"},
														   { text:'Tipi rete' , leaf:true , iconCls:'icon-rete',href:"javascript:app.showGridTipiWindow(trete.window)"},
														   { text:'Tipi trasmissione' , leaf:true , iconCls:'icon-trasmissione',href:"javascript:app.showGridTipiWindow(ttrasmissione.window)"},
														   { text:'Tipi vallo' , leaf:true , iconCls:'icon-vallo',href:"javascript:app.showGridTipiWindow(tvallo.window)"}  
															
												]
											}]
										//*************************    children
										
										
									})
								
								//****   treepanel
							}]
							
						},
						{
							title:'Strade/Comuni',
							frame:true,
							border:false,
							items :[{ //****   treepanel
								xtype:'treepanel',
								id:'im-tree', 
								loader: new Ext.tree.TreeLoader(),
								rootVisible:false,
								lines:false,
								autoScroll:true,
								border:false,	
								frame:true,
								root: new Ext.tree.AsyncTreeNode({
									text:'Online',
									
									//*************************    children
									children:[{   	
											text:'Tabelle', expanded:true,
											children:[	 
													   { text:'Strade' , leaf:true , iconCls:'icon-confg' , href:"javascript:app.showGridTipiWindow(talimentazioni.window)"},
													   { text:'Comuni' , leaf:true , iconCls:'icon-ancoraggio',href:"javascript:app.showGridTipiWindow(tancoraggi.window)"} 
											]
										}]
									//*************************    children
									
									
								})
							 
							}]//****   treepanel

							
						}] //tab panel items
					}] //application-left-panel
			},
			
			
			{
				xtype:'grid',
				
				bbar : new Ext.PagingToolbar({ 
					pageSize: Ext.GRID_PAGE_SIZE, 
					displayInfo: true,
					displayMsg: 'Schede dalla <b>{0}</b> alla <b>{1}</b> su un totale di <b>{2}</b>',
					store : opere.ds, 
					emptyMsg: "Niente da visualizzare" 
				}) ,
				
				id:'schede-grid',
				loadMask: {msg:'Caricamento in corso ...'},
				ds:  opere.ds,
				title : 'Elenco Opere' ,
				listeners: {
				    rowdblclick : function(grid , rowIndex, event){
						var record = grid.store.getAt(rowIndex) ;  
						app.showOpereWindow(record);
						
					}
				},
				cm: new Ext.grid.ColumnModel([   
							{ 	header: 'Tipo' ,  dataIndex: 'Tipo',   width: 40 ,  sortable:true  }, 
							{ 	header: 'N°' ,  dataIndex: 'Numero',   width: 40 ,  sortable:true  }, 
							{ 	header: 'Est.' ,  dataIndex: 'Estensione',   width: 40 ,  sortable:true  }, 
							{ 	header: 'Strada' ,  dataIndex: 'DescStrada',   width: 130 ,  sortable:true  }, 
							{ 	header: 'Comune' ,  dataIndex: 'DescComune',   width: 130 ,  sortable:true  }, 
							{ 	header: 'Prov.' ,  dataIndex: 'Siglaprovincia',   width: 40 ,  sortable:true  }, 
							{ 	header: 'Località' ,  dataIndex: 'Localita',   width: 130 ,  sortable:true  }, 
							{ 	header: 'Km' ,  dataIndex: 'Km',   width: 50   }, 
							{ 	header: 'Metri' ,  dataIndex: 'Metri',   width: 50   }, 
							{ 	header: 'Long.' ,  dataIndex: 'Longitudine',   width: 50   }, 
							{ 	header: 'Lat.' ,  dataIndex: 'Latitudine',   width: 50   }, 
							{ 	header: 'Dist.' ,  dataIndex: 'Distanzasedestradale',   width: 50   }, 
							{ 	header: 'Quota' ,  dataIndex: 'Quotaslm',   width: 50   }
					]),
					
				viewConfig: {
					forceFit:false
				}, 
				collapsible: false , 
				animCollapse: false ,
				region: 'center' ,
				margins:'2 4 4 2',
				iconCls:'icon-list',	
				
				autoScroll  : true 
			}
			
			
		]} 
	
	);
	
	//mapping Trova button
	new Ext.KeyMap("schede-search-panel", { key: 13,  fn: Ext.getCmp("button-schede-search-trova").handler , scope: Ext.getCmp("schede-search-panel") });
	Ext.get(document.body).unmask(); 
});